{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<table class=\"ee-notebook-buttons\" align=\"left\">\n",
    "    <td><a target=\"_blank\"  href=\"https://github.com/giswqs/geemap/tree/master/examples/notebooks/earthengine_js_to_ipynb.ipynb\"><img width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /> View source on GitHub</a></td>\n",
    "    <td><a target=\"_blank\"  href=\"https://nbviewer.jupyter.org/github/giswqs/geemap/blob/master/examples/notebooks/earthengine_js_to_ipynb.ipynb\"><img width=26px src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Jupyter_logo.svg/883px-Jupyter_logo.svg.png\" />Notebook Viewer</a></td>\n",
    "    <td><a target=\"_blank\"  href=\"https://mybinder.org/v2/gh/giswqs/geemap/master?filepath=examples/notebooks/earthengine_js_to_ipynb.ipynb\"><img width=58px src=\"https://mybinder.org/static/images/logo_social.png\" />Run in binder</a></td>\n",
    "    <td><a target=\"_blank\"  href=\"https://colab.research.google.com/github/giswqs/geemap/blob/master/examples/notebooks/earthengine_js_to_ipynb.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /> Run in Google Colab</a></td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Automatic conversion from Earth Engine JavaScripts to Python scripts\n",
    "\n",
    "### Install Earth Engine API and geemap\n",
    "Install the [Earth Engine Python API](https://developers.google.com/earth-engine/python_install) and [geemap](https://github.com/giswqs/geemap). The **geemap** Python package is built upon the [ipyleaflet](https://github.com/jupyter-widgets/ipyleaflet) and [folium](https://github.com/python-visualization/folium) packages and implements several methods for interacting with Earth Engine data layers, such as `Map.addLayer()`, `Map.setCenter()`, and `Map.centerObject()`.\n",
    "The following script checks if the geemap package has been installed. If not, it will install geemap, which automatically installs its [dependencies](https://github.com/giswqs/geemap#dependencies), including earthengine-api, folium, and ipyleaflet.\n",
    "\n",
    "**Important note**: A key difference between folium and ipyleaflet is that ipyleaflet is built upon ipywidgets and allows bidirectional communication between the front-end and the backend enabling the use of the map to capture user input, while folium is meant for displaying static data only ([source](https://blog.jupyter.org/interactive-gis-in-jupyter-with-ipyleaflet-52f9657fa7a)). Note that [Google Colab](https://colab.research.google.com/) currently does not support ipyleaflet ([source](https://github.com/googlecolab/colabtools/issues/60#issuecomment-596225619)). Therefore, if you are using geemap with Google Colab, you should use [`import geemap.eefolium`](https://github.com/giswqs/geemap/blob/master/geemap/eefolium.py). If you are using geemap with [binder](https://mybinder.org/) or a local Jupyter notebook server, you can use [`import geemap`](https://github.com/giswqs/geemap/blob/master/geemap/geemap.py), which provides more functionalities for capturing user input (e.g., mouse-clicking and moving)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import subprocess\n",
    "\n",
    "try:\n",
    "    import geemap\n",
    "except ImportError:\n",
    "    print('geemap package not installed. Installing ...')\n",
    "    subprocess.check_call([\"python\", '-m', 'pip', 'install', 'geemap'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Convert Earth Eninge JavaScripts to Python scripts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Converting Earth Engine JavaScripts to Python scripts...\n",
      "\n",
      "Processing 1/5: /home/qiusheng/geemap/ModisQaBands.js\n",
      "Processing 2/5: /home/qiusheng/geemap/FromName.js\n",
      "Processing 3/5: /home/qiusheng/geemap/NormalizedDifference.js\n",
      "Processing 4/5: /home/qiusheng/geemap/QualityMosaic.js\n",
      "Processing 5/5: /home/qiusheng/geemap/ClippedComposite.js\n",
      "Python scripts saved at: /home/qiusheng/geemap\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from geemap.conversion import *\n",
    "\n",
    "# Create a temporary working directory\n",
    "work_dir = os.path.join(os.path.expanduser('~'), 'geemap')\n",
    "# Get Earth Engine JavaScript examples. There are five examples in the geemap package folder. \n",
    "# Change js_dir to your own folder containing your Earth Engine JavaScripts, \n",
    "# such as js_dir = '/path/to/your/js/folder'\n",
    "js_dir = get_js_examples(out_dir=work_dir) \n",
    "\n",
    "# Convert all Earth Engine JavaScripts in a folder recursively to Python scripts.\n",
    "js_to_python_dir(in_dir=js_dir, out_dir=js_dir, use_qgis=True)\n",
    "print(\"Python scripts saved at: {}\".format(js_dir))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Convert Earth Engine Python scripts to Jupyter Notebooks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Converting Earth Engine Python scripts to Jupyter notebooks ...\n",
      "\n",
      "Processing 1/5: /home/qiusheng/geemap/ModisQaBands_qgis.py\n",
      "\n",
      "Processing 2/5: /home/qiusheng/geemap/QualityMosaic_qgis.py\n",
      "\n",
      "Processing 3/5: /home/qiusheng/geemap/FromName_qgis.py\n",
      "\n",
      "Processing 4/5: /home/qiusheng/geemap/NormalizedDifference_qgis.py\n",
      "\n",
      "Processing 5/5: /home/qiusheng/geemap/ClippedComposite_qgis.py\n",
      "\n",
      "Executing Earth Engine Jupyter notebooks ...\n",
      "\n",
      "Processing 1/5: /home/qiusheng/geemap/NormalizedDifference.ipynb ...\n",
      "\n",
      "Processing 2/5: /home/qiusheng/geemap/FromName.ipynb ...\n",
      "\n",
      "Processing 3/5: /home/qiusheng/geemap/QualityMosaic.ipynb ...\n",
      "\n",
      "Processing 4/5: /home/qiusheng/geemap/ClippedComposite.ipynb ...\n",
      "\n",
      "Processing 5/5: /home/qiusheng/geemap/ModisQaBands.ipynb ...\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Convert all Earth Engine Python scripts in a folder recursively to Jupyter notebooks.\n",
    "nb_template = get_nb_template()  # Get the notebook template from the package folder.\n",
    "py_to_ipynb_dir(js_dir, nb_template)\n",
    "\n",
    "# Execute all Jupyter notebooks in a folder recursively and save the output cells.\n",
    "execute_notebook_dir(in_dir=js_dir)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
